<template>
  <div class="store-home">
    <SearchBar></SearchBar>
    <!-- :data="random"  这就是我们要随机要显示的图书，将这本书传给我们flapcard -->
    <FlapCard :data="random"></FlapCard>
    <Scroll :top="scrollTop" :bottom="0" @onScroll="onScroll" ref="scroll">
      <div class="banner-wrapper">
        <div class="banner-img" :style="{backgroundImage:`url('${banner}')`}"></div>
      </div>
      <guess-you-like :data="guessYouLike"></guess-you-like>
      <recommend :data="recommend" class="recommend"></recommend>
      <featured :data="featured" :titleText="$t('home.featured')" :btnText="$t('home.seeAll')" class="featured"></featured>
      <div class="category-list-wrapper" v-for="(item, index) in categoryList" :key="index">
        <category-book :data="item"></category-book>
      </div>
      <category class="categories" :data="categories"></category>
    </Scroll>
  </div>
</template>

<script>
  import SearchBar from '../../components/home/SearchBar'
  import Scroll from '../../components/common/Scroll'
  import FlapCard from '../../components/home/FlapCard'
  import { storeHomeMixin } from '../../utils/mixin'
  import { home } from '../../api/store'
  import GuessYouLike from '../../components/home/GuessYouLike'
  import Recommend from '../../components/home/Recommend'
  import Featured from '../../components/home/Featured'
  import CategoryBook from '../../components/home/CategoryBook'
  import Category from '../../components/home/Category'
  export default {
    mixins: [storeHomeMixin],
    components: {
      Category,
      CategoryBook,
      Featured,
      Recommend,
      GuessYouLike,
      SearchBar,
      Scroll,
      FlapCard
    },
    data() {
      return {
        scrollTop: 188, // 94
        random: null,
        banner: null,
        guessYouLike: null,
        recommend: null,
        featured: null,
        categoryList: null,
        categories: null
      }
    },
    methods: {
      // 通过onScroll()接受Y轴的偏移量
      onScroll(offsetY) {
        // console.log(offsetY)
        this.setOffsetY(offsetY)
        if (offsetY > 0) {
          this.scrollTop = 104// 52
        } else {
          this.scrollTop = 188// 94
        }
        this.$refs.scroll.refresh()
      }
    },
    mounted() {
      console.log('开始获取书籍信息')
      home().then(response => {
        // console.log(response)
        const data = response
        console.log('书城页面需要的数据', data)
        // const randomIndex = Math.floor(Math.random() * data.random.length)
        // this.random = data.random[randomIndex]
        this.random = data.random[0]
        // console.log('random', this.random)
        // console.log(this.random)
        // 取到bannerde 图片
        this.banner = data.banner
        this.guessYouLike = data.guessYouLike
        // console.log(this.guessYouLike)
        this.recommend = data.recommend
        // console.log(this.recommend)
        this.featured = data.featured
        // console.log(this.featured)
        this.categoryList = data.categoryList
        // console.log(this.categoryList)
        this.categories = data.categories
        // console.log(this.categories)
      })
    }
  }
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
  @import "../../assets/styles/global";
  .store-home {
    width: 100%;
    height: 100%;
    .banner-wrapper {
      width: 100%;
      padding: px2rem(20); // 10
      box-sizing: border-box;
      .banner-img {
        width: 100%;
        height: px2rem(300);// 150
        // 适应屏幕chicun
        background-repeat: no-repeat;
        background-size: 100% 100%;
      }
    }
    .recommend {
      margin-top: px2rem(40);// 20;
    }
    .featured {
      margin-top: px2rem(40);// 20;
    }
    .category-list-wrapper {
      margin-top: px2rem(40);// 20;
    }
    .categories {
      margin-top: px2rem(40);// 20;
    }
  }
</style>
